﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CI.Models;
using CI.Models.Data;
using CI.Models.Production;
using Northwoods.GoXam;
using Northwoods.GoXam.Model;
using Orbifold.G2.Core;
using QuickGraph;

namespace CI.UI.Prototypes
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class GraphSharpLayoutWindow : Window
    {
        public BidirectionalGraph<object, IEdge<object>> Graph
        {
            get; set;
        }

        private IOwner _root = Models.Owner.Create("Amminous");
        private ProductionViewModel _model;
        private PIJobModelManager _manager = new PIJobModelManager() { ProductionRepo = App.ItemRepository};

        public GraphSharpLayoutWindow()
        {
            

            InitializeComponent();
            _model = new ProductionViewModel(App.ItemRepository);
            this.DataContext = _model;

            foreach (var a in _model.LayoutAlgorithms) LayoutAlgorithms.Items.Add(a);
            /*
            myDiagram.Model = dModel;
            myDiagram.Model.NodesSource = new List<ProductionJob>();

            // data-bind Slider.Value to Diagram.Panel.Scale, once the Diagram.Panel
            // has been created by expanding the Diagram's ControlTemplate
            myDiagram.TemplateApplied += (s, e) =>
            {
                var b = new Binding("Scale")
                            {
                                Source = myDiagram.Panel,
                                Mode = BindingMode.TwoWay
                            };
                myScaleSlider.SetBinding(RangeBase.ValueProperty, b);
            };

            this.Loaded += (s, e) => { myOverview.Observed = myDiagram; };
             */
        }

        private void AddItem_Click(object sender, RoutedEventArgs e)
        {
            Graph = new BidirectionalGraph<object, IEdge<object>>();

            Graph.AddVertex(_root);

            var job = _manager.AddChainFor(ItemSelector.SelectedItem as Item,
                                                       new PIModelOptions() {MinimumManufacturingLevel = PIManufacturingLevel.Resource});
            AddShape(job,_root);

            graphLayout.Graph = Graph;
            graphLayout.Relayout();
			Zoom.ZoomToFill();
        }

        private void AddShape(IJob job, object parent)
        {
            /*
            myDiagram.StartTransaction("AddNewItem");
            myDiagram.Model.AddNode(job);
            myDiagram.CommitTransaction("AddNewItem");
            

            
             */
            Graph.AddVertex(job);

            if (parent != null)
            {
                // check for existing edge?!
                IEdge<object> edge;
                if (!Graph.TryGetEdge(job,parent,out edge))
                {
                    Graph.AddEdge(new Edge<object>(job, parent));    
                }
                
            }

            foreach (var c in job.ChildJobs)
                AddShape(c, job);
            /*
            Graph.AddVertex(job.Output.Item.TypeName);

            if (parent != null) Graph.AddEdge(new Edge<object>(job.Output.Item.TypeName, parent.Output.Item.TypeName));

            foreach (var c in job.ChildJobs)
                AddShape(c, job);
            */
            /*
            Graph.AddVertex(job);

            if (parent != null) Graph.AddEdge(new Edge<IIndustryJob>(job, parent));


            foreach (var c in job.ChildJobs)
                AddShape(c, job);*/
        }

        private void RedrawButton_Click(object sender, RoutedEventArgs e)
        {
            graphLayout.Relayout();
			Zoom.ZoomToFill();
        }

        private void LayoutAlgorithms_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            graphLayout.LayoutAlgorithmType = LayoutAlgorithms.SelectedItem as string;
            graphLayout.Relayout();
			Zoom.ZoomToFill();
        }
    }

}
